<?php
    /*
    * @copyright        [YouNet_COPYRIGHT]
    * @author           YouNet Development
    * @package          Module_Contactimporter
    * @version          2.06
    *
    */
    defined('PHPFOX') or exit('NO DICE!');
?>
<?php
if (file_exists(PHPFOX_DIR.'module'.PHPFOX_DS.'contactimporter'.PHPFOX_DS.'include'.PHPFOX_DS.'component'.PHPFOX_DS.'controller'.PHPFOX_DS.'openinviter'.PHPFOX_DS.'openinviter.php'))
{
    require_once(PHPFOX_DIR.'module'.PHPFOX_DS.'contactimporter'.PHPFOX_DS.'include'.PHPFOX_DS.'component'.PHPFOX_DS.'controller'.PHPFOX_DS.'openinviter'.PHPFOX_DS.'openinviter.php');
}
require_once (PHPFOX_DIR . 'module' . PHPFOX_DS . 'contactimporter' . PHPFOX_DS . 'include' . PHPFOX_DS . 'service' . PHPFOX_DS . 'abstract.class.php');
class Contactimporter_Service_Gmail extends Contactimporter_Service_Abstract
{
    private $_provider = 'gmail';

    public function get($iPage = 1, $iLimit = 50)
	{
		$iCnt = 0;
        $aErrors = $aRows = $aMails = $aInviteList = $aJoined = array();
        if (!isset($_SESSION['contactimporter'][$this->_provider]))
        {
            $sEmail = isset($_POST['email_box']) ? $_POST['email_box'] : '';
            $sPassword = isset($_POST['password_box']) ? $_POST['password_box'] : '';
            $oOpenInviter = null;
            if (!isset($oOpenInviter) && $oOpenInviter == null)
            {
                $oOpenInviter = new openinviter();
            }            
            $oOpenInviter->startPlugin($this->_provider, true);
            $aInternal = $oOpenInviter->getInternalError();
            if ($aInternal != null)
            {
                $aErrors['inviter'] = $aInternal;
            }
            elseif (!$oOpenInviter->login($sEmail, $sPassword))
            {
                $aInternal = $oOpenInviter->getInternalError();
                $aErrors['login'] = $aInternal ? $aInternal: Phpfox::getPhrase('contactimporter.login_failed_please_check_the_email_and_password_you_have_provided_and_try_again_later');
            }
            elseif (false === $aContacts = $oOpenInviter->getMyContacts())
            {
                $aErrors['contacts'] = Phpfox::getPhrase('contactimporter.unable_to_get_contacts');
            }
            $_SESSION['contactimporter'][$this->_provider] = $aContacts;
            if (count($aContacts) <= 0)
            {
                $aErrors['contacts'] = Phpfox::getPhrase('contactimporter.there_is_not_contact_in_your_account');
            }
        }
        else
        {
            $aContacts = $_SESSION['contactimporter'][$this->_provider];
        }
        if (!$aErrors)
        {
            $iOffset = ($iPage - 1) * $iLimit;
            $aRows = $aMails = $aInviteList = array();
            if (!$aContacts || count($aContacts) <= 0)
            {
                return array();
            }
            $aRows = array();
            foreach ($aContacts as $sEmail => $sFullName)
            {
                $aRow['name'] = $sFullName;
                $aRow['email'] = $sEmail;
                $aMails[] = $sEmail;
                $aRows[] = $aRow;
            }
            list($aMails, $aInvalid, $aJoined) = Phpfox::getService('invite')->getValid($aMails, Phpfox::getUserId());
            $aInviteList = array();
            foreach ($aRows as $aRow)
            {
                if (in_array($aRow['email'], $aMails))
                {
                    $aInviteList[] = $aRow;                    
                }
            }            
            $iCnt = count($aInviteList);
            $_SESSION['contactimporter'][$this->_provider . '_invite_list'] = array_values($aMails);
            $aInviteList = array_slice($aInviteList, $iOffset, $iLimit);
            $aInviteList = $this->processEmailRows($aInviteList);
        }
        return array($iCnt, $aInviteList, $aJoined, $aErrors);
    }
}
?>